package com.eastcom.notice;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class NoticeSender {
	private static NoticeSender instance = null;

	private int sendCount = 2;

	private Lock lock = new ReentrantLock();
	

	public Lock getLock() {
		return this.lock;
	}

	public Lock getNewLock()
	{
		return new ReentrantLock();
	}
	
	private NoticeSender() {
	}

	private NoticeSender(int sendCount) {
		this.sendCount = sendCount;
	}

	public static NoticeSender getInstance() {
		if (instance == null) {
			synchronized (NoticeSender.class) {
				if (instance == null) {
					instance = new NoticeSender();
				}
				NoticeSender.class.notifyAll();
			}
		}

		return instance;
	}

	public static NoticeSender getInstance(int sendCount) {
		if (instance == null) {
			synchronized (NoticeSender.class) {
				if (instance == null) {
					instance = new NoticeSender(sendCount);
				}
				NoticeSender.class.notifyAll();
			}
		}

		return instance;
	}

	public boolean sendObject(String host, int port, Object object) {
		int sendIndex = 0;

		Socket socket = null;
		OutputStream outStream = null;
		ObjectOutputStream objWriter = null;
		boolean stop = false;
		boolean success = false;

		while ((sendIndex < this.sendCount) && (!(stop))) {
			try {
				socket = new Socket(host, port);
				outStream = socket.getOutputStream();
				objWriter = new ObjectOutputStream(outStream);
				objWriter.writeObject(object);
				stop = true;
				success = true;
			} catch (Exception e) {
				System.out.println("NoticeSende sendObject error "
						+ e.getMessage());
				e.printStackTrace();
				++sendIndex;
			}
			doClose(null, null, objWriter, outStream, socket);
		}

		return success;
	}

	public boolean sendObject(String host, int port, Object object,
			boolean locked) {
		int sendIndex = 0;
		Socket socket = null;
		OutputStream outStream = null;
		ObjectOutputStream objWriter = null;
		boolean stop = false;
		boolean success = false;

		if (locked) {
			this.lock.lock();
		}
		while ((sendIndex < this.sendCount) && (!(stop))) {
			try {
				socket = new Socket(host, port);
				outStream = socket.getOutputStream();
				objWriter = new ObjectOutputStream(outStream);
				objWriter.writeObject(object);
				stop = true;
				success = true;
			} catch (Exception e) {
				System.out.println("NoticeSende sendObject error "
						+ e.getMessage());
				e.printStackTrace();
				++sendIndex;
			}
			doClose(null, null, objWriter, outStream, socket);
		}
		if (locked) {
			this.lock.unlock();
		}
		return success;
	}

	public void sendObject(ObjectOutputStream objWriter, Object object)
			throws Exception {
		try {
			objWriter.writeObject(object);
		} catch (Exception e) {
			throw e;
		}
	}

	public void doClose(ObjectInputStream objReader, InputStream inStream,
			ObjectOutputStream objWriter, OutputStream outStream, Socket socket) {
		if (inStream != null)
			try {
				inStream.close();
			} catch (IOException localIOException) {
			}
		if (objReader != null)
			try {
				objReader.close();
			} catch (IOException localIOException1) {
			}
		if (outStream != null)
			try {
				outStream.close();
			} catch (IOException localIOException2) {
			}
		if (objWriter != null)
			try {
				objWriter.close();
			} catch (IOException localIOException3) {
			}
		if (socket == null)
			return;
		try {
			socket.close();
		} catch (IOException localIOException4) {
		}
	}

	public static void main(String[] args) {
		NoticeSender sender = getInstance();
		for (int i = 0; i < 13; ++i)
			sender.sendObject("127.0.0.1", 1000, new String(String.valueOf(i)));
	}
}